package it.trekker.map;

import com.google.android.maps.OverlayItem;
import it.trekker.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MapPopupView<Item extends OverlayItem> extends FrameLayout {

//	private static final int THUMBNAIL_HEIGHT = 70;
//	private static final int THUMBNAIL_WIDTH = 70;

	private LimitLinearLayout layout;
	private TextView title_tv;
	private ImageView img_iv;


	public MapPopupView(Context context) {
		super(context);
		
		Drawable d = getResources().getDrawable(R.drawable.polaroid_frame);
		int backgroundHeight = d.getIntrinsicHeight();
		int backgroundWidth = d.getIntrinsicWidth();
		
		layout = new LimitLinearLayout(context);
		layout.setVisibility(VISIBLE);

		setupView(context, layout);

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.TOP;
		params.setMargins(0, 0, 0, 0);

		addView(layout, params);
	}

	
	public int getCustomHeight() {
		return getResources().getDrawable(R.drawable.polaroid_frame).getMinimumHeight()*2;
	}
	
	
	public int getCustomWidth() {
		return getResources().getDrawable(R.drawable.polaroid_frame).getMinimumWidth()*2;
	}


	private void setupView(Context context, final ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.photo_popup_layout, parent);

		title_tv = (TextView) v.findViewById(R.id.title);
		img_iv = (ImageView) v.findViewById(R.id.photo_view);
	}


	public void setData(Item item) {
		title_tv.setText(item.getTitle());
		img_iv.setImageBitmap(createThumbnail(item.getSnippet()));
	}


	private Bitmap createThumbnail(String path) {
		
		Drawable d = getResources().getDrawable(R.drawable.polaroid_frame);
		int backgroundWidth = d.getMinimumWidth();
		int backgroundHeight = d.getMinimumHeight();
		
		int thumbnailWidth = backgroundWidth - 10;
		int thumbnailHeight = backgroundHeight - 40;
		
		// Retrieve image as bitmap
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inSampleSize = 20;
		Bitmap img = BitmapFactory.decodeFile(path, opt);
		
		double rate;
		int scaledHeight;
		int scaledWidth;
		
		if(img.getWidth() < img.getHeight()) {
			rate = (double)img.getHeight()/(double)img.getWidth();
			scaledWidth = thumbnailWidth;
			scaledHeight = (int) (scaledWidth * rate);
		}
		else {
			rate = (double)img.getWidth()/(double)img.getHeight();
			scaledHeight = thumbnailHeight;
			scaledWidth = (int) (scaledHeight * rate);
		}
		
		Bitmap scaledImg = Bitmap.createScaledBitmap(img, scaledWidth, scaledHeight, false);
		//		Matrix m = new Matrix();
		//		m.postRotate(-12);
		Bitmap thumbnail = Bitmap.createBitmap(
				scaledImg, scaledWidth/2-thumbnailWidth/2, scaledHeight/2-thumbnailHeight/2, 
				thumbnailWidth, thumbnailHeight, null, false
		);
		
		int imgOffsetX = backgroundWidth/2 - thumbnailWidth/2;
		int imgOffsetY = 10;
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		lp.setMargins(imgOffsetX, imgOffsetY, 0, 0);
		img_iv.setLayoutParams(lp);
		
		return thumbnail;
	}




	//*******************************************************************//
	//********************* LimitLinearLayout class *********************//
	//*******************************************************************//

	private class LimitLinearLayout extends LinearLayout {

		public LimitLinearLayout(Context context) {
			super(context);
		}

		public LimitLinearLayout(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

			Drawable d = getResources().getDrawable(R.drawable.polaroid_frame);
			int backgroundWidth = d.getMinimumWidth();
			int backgroundHeight = d.getMinimumHeight()*2;

			super.onMeasure(backgroundWidth, backgroundHeight);
		}
	}
}

